package com.bs.visitservice.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bs.visitservice.entity.Seller;
import com.bs.visitservice.service.SellerService;import com.bs.visitservice.utils.ParamUtils;
import com.bs.visitservice.utils.Result;
import com.bs.visitservice.utils.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 核查店铺申请信息
 */
@RestController
@RequestMapping("/admin")
public class CheckSellerInfoController {
    @Autowired
    private SellerService sellerService;

    /**
     * 获取商家店铺信息
     * @param cond
     * @return
     */
    @RequestMapping(value = "querySellerInfo",method = RequestMethod.POST)
    public Result querySellerInfo(@RequestBody Map cond){
        Seller seller = (Seller)ResultUtil.transObject(cond,Seller.class);
        IPage page = ParamUtils.getPage(cond);
        LambdaQueryWrapper<Seller> wrapper = new LambdaQueryWrapper<Seller>();
        wrapper.ne(Seller::getStatus,4);
        if(seller.getStatus()!=null){
            wrapper.eq(Seller::getStatus,seller.getStatus());
        }
        if(seller.getSellerNo()!=null){
            wrapper.eq(Seller::getSellerNo,seller.getSellerNo());
        }
        if(StringUtils.isNotEmpty(seller.getSellerName())){
            wrapper.like(Seller::getSellerName,seller.getSellerName());
        }
        if(StringUtils.isNotEmpty(seller.getServeType())){
            wrapper.eq(Seller::getServeType,seller.getServeType());
        }
        wrapper.orderByDesc(Seller::getRegistDate);
        page = sellerService.pageMaps(page, wrapper);
        List<Map<String,Object>> records = page.getRecords();
        ResultUtil.transfort(records,getDictMap(),true);
        Map<String,Object> result = new HashMap<>();
        result.put("total",page.getTotal());
        result.put("datas",records);
        return Result.success(result);
    }

    /**
     * 更新店铺状态，封禁或者审核通过/失败
     * @param cond
     * @return
     */
    @RequestMapping(value = "/updateSellerInfo",method = RequestMethod.POST)
    public Result updateSellerInfo(@RequestBody Map cond){
        Seller seller = (Seller)ResultUtil.transObject(cond, Seller.class);
        LambdaUpdateWrapper<Seller> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Seller::getStatus,seller.getStatus());
        wrapper.eq(Seller::getSellerNo,seller.getSellerNo());
        boolean isSuc = sellerService.update(wrapper);
        if(isSuc){
            return Result.success(null);
        }

        return Result.error(null);

    }
    /**
     * 获取列和字典值的映射关系
     * @return
     */
    private Map getDictMap(){
        Map<String,String> dictMap = new HashMap<>();
        dictMap.put("status","sellerStatus");
        dictMap.put("serveType","serveType");
        return dictMap;
    }
}
